package com.example.controller;

import com.example.pojo.vo.*;
import com.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl service;

//    @PostMapping("/find")
//    public Result find(@RequestBody QueryPageBean queryPageBean) {
//        System.out.println("开始查找");
//        PageResult pageResult = service.selectByPage(queryPageBean);
//        return new Result(true, "查询成功", pageResult);
//    }
    @GetMapping("/find/{currentPage}/{pageSize}/{username}")
    public Result find(@PathVariable Integer currentPage,
                       @PathVariable Integer pageSize,
                       @PathVariable String username){
        Result result = getResult(currentPage, pageSize, username);
        return  result;
    }
    @GetMapping("/find/{currentPage}/{pageSize}")
    public Result find1(@PathVariable Integer currentPage,
                       @PathVariable Integer pageSize)
    {
        Result result= getResult(currentPage, pageSize,"");
        return  result;
    }
    @PostMapping("/add")
    public Result add(@RequestBody AddUser au){
        service.addUser(au);
        return new Result(true,"添加成功");
    }
    @PutMapping("/updata")
    public Result updata(@RequestBody UpdateUser uu){
        service.updateUser(uu);
        return new Result(true,"修改成功");
    }
    @DeleteMapping("/delete/{uid}")
    public Result delete(@PathVariable String uid){
        service.deleteUser(uid);
        return new Result(true,"删除成功");
    }

    private Result getResult(Integer currentPage, Integer pageSize, String username) {
        QueryPageBean queryPageBean=new QueryPageBean();
        queryPageBean.setCurrentPage(currentPage);
        queryPageBean.setPageSize(pageSize);
        HashMap<String,String>m=new HashMap<>();
        m.put("username", username);
        queryPageBean.setQueryParams(m);
        PageResult pageResult = service.selectByPage(queryPageBean);
        Result result=new Result(true,"查询成功",pageResult);
        return result;
    }
}
